package com.scedu.handler;

import org.springframework.security.access.AccessDeniedException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;

@ControllerAdvice
public class GlobalErrorHandler {
    @ExceptionHandler(value = Exception.class)
    public ModelAndView handleException(Exception e, HttpServletRequest request) {
        ModelAndView modelAndView=new ModelAndView();
        //获取异常信息，放入request域中，（基本不用）
        modelAndView.addObject("errorMsg", e.getMessage());
        if (e instanceof AccessDeniedException) {

            modelAndView.setViewName("forward:/403");//不经过视图解析器，地址栏不变，页面转发
        }else{
            modelAndView.setViewName("redirect:/500");//不经过视图解析器，地址栏变，页面重定向
        }
        return modelAndView;
    }
}
